package com.woniu108.product.controller;


import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.dto.ProductBrandDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.product.param.*;
import com.woniu108.product.service.ProductBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品品牌表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/productBrand")
@Api(tags = "商品品牌接口")
public class ProductBrandController {
    @Resource
    public ProductBrandService productBrandService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 查询所有商品品牌
     *
     * @return 品牌详情dto集合
     */
    @GetMapping("/findAllProductBrand.api")
    @ApiOperation("查询所有商品品牌")
    public Result<List<ProductBrandDto>> findAllProductBrand(@RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        List<ProductBrandDto> dtos = productBrandService.findAllProductBrand(adminId);
        return Result.newListInstance(ProductBrandDto.class).setData(dtos).setMsg("查询所有商品品牌成功");
    }

    /**
     * 查询所有正常商品品牌
     *
     * @return 品牌详情dto集合
     */
    @GetMapping("/findAllNormalProductBrand.c")
    @ApiOperation("查询所有正常商品品牌")
    public Result<List<ProductBrandDto>> findAllNormalProductBrand() {
        List<ProductBrandDto> dtos = productBrandService.findAllNormalProductBrand();
        return Result.newListInstance(ProductBrandDto.class).setData(dtos).setMsg("查询所有正常商品品牌成功");
    }

    /**
     * 新增商品品牌
     *
     * @param pBName 品牌名
     * @return boolean值
     */
    @GetMapping("/insertProductBrand.api")
    @ApiOperation("新增商品品牌")@AdminHistoryLog
    @ApiImplicitParam(name = "pBName", value = "品牌名")
    public Result<Boolean> insertProductBrand(String pBName, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        InsertProductBrandParam param = new InsertProductBrandParam(pBName, adminId);
        Boolean bool = productBrandService.insertProductBrand(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增商品品牌成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增商品品牌失败").setData(false).setCode(500);
    }

    /**
     * 修改商品品牌名
     *
     * @param pBId   品牌id
     * @param pBName 新品牌名
     * @return boolean值
     */
    @GetMapping("/updateProductBrand.api")
    @ApiOperation("修改商品品牌名")@AdminHistoryLog
    public Result<Boolean> updateProductBrand(Integer pBId, String pBName, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UpdateProductBrandParam param = new UpdateProductBrandParam();
        param.setPBId(pBId);
        param.setPBName(pBName);
        param.setAdminId(adminId);
        Boolean bool = productBrandService.updateProductBrand(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改商品品牌名成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改商品品牌名失败").setData(false).setCode(500);
    }

    /**
     * 禁用商品品牌
     *
     * @param pBIds 品牌id集合
     * @return boolean值
     */
    @PostMapping("/banProductBrand.api")
    @ApiOperation("禁用商品品牌")@AdminHistoryLog
    @ApiImplicitParam(name = "pBIds", value = "品牌id集合")
    public Result<Boolean> banProductBrand(@RequestBody List<Integer> pBIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        BanProductBrandParam param = new BanProductBrandParam(pBIds, adminId);
        Boolean bool = productBrandService.banProductBrand(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("禁用商品品牌成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("禁用商品品牌失败").setData(false).setCode(500);
    }

    /**
     * 解锁商品品牌
     *
     * @param pBIds 品牌id集合
     * @return boolean值
     */
    @PostMapping("/unlockProductBrand.api")
    @ApiOperation("解锁商品品牌")@AdminHistoryLog
    @ApiImplicitParam(name = "pBIds", value = "品牌id集合")
    public Result<Boolean> unlockProductBrand(@RequestBody List<Integer> pBIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UnlockProductBrandParam param = new UnlockProductBrandParam(pBIds, adminId);
        Boolean bool = productBrandService.unlockProductBrand(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("解锁商品品牌成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("解锁商品品牌失败").setData(false).setCode(500);
    }

    /**
     * 删除商品品牌
     *
     * @param pBIds 品牌id集合
     * @return boolean值
     */
    @PostMapping("/deleteProductBrand.api")
    @ApiOperation("删除商品品牌")@AdminHistoryLog
    @ApiImplicitParam(name = "pBIds", value = "品牌id集合")
    public Result<Boolean> deleteProductBrand(@RequestBody List<Integer> pBIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        DeleteProductBrandParam param = new DeleteProductBrandParam(pBIds, adminId);
        Boolean bool = productBrandService.deleteProductBrand(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除商品品牌成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除商品品牌失败").setData(false).setCode(500);
    }
}

